package org.mydba.report.gui;

import java.util.List;
import java.util.ArrayList;

public class Hint {
    private int x;
    private int y;
    private int widthPlusX;
    private int width;
    private int heightPlusY;
    private List<Long> sqlExecIds = new ArrayList<Long>(1);
    private int type;

    Hint(int x, int y, int width, int height, long sqlExecId, int type) {
        this.x = x;
        this.y = y;
        this.widthPlusX = width+x;
        this.width = width;
        this.heightPlusY = height+y;
        this.sqlExecIds.add(sqlExecId);
        this.type = type;
    }

    public int getX() {
        return x;
    }

    public long getSqlExecId() {
        return sqlExecIds.get(0);
    }

    public List<Long> getSqlExecIds() {
        return sqlExecIds;
    }

    public int getType() {
        return type;
    }

    public boolean contains(int mouseX, int mouseY) {
        return mouseX >= x && mouseY >= y && mouseX <= widthPlusX && mouseY <= heightPlusY;
    }

    /**
     * Compare this new hint to existing one and reduce x so that it the new Hint will not overlap the old one.
     *
     * @param overlappedOldHint
     */
    public int doNotOverlap(final Hint overlappedOldHint) {
        if( this.x <= overlappedOldHint.widthPlusX && this.x >= overlappedOldHint.x ) {
            // System.out.println("OVERLAP DETECTED, "+this+" will overlap "+overlappedOldHint);
            final int oldX = this.x;
            this.x = overlappedOldHint.widthPlusX + 1;

            if( width - x - oldX <= 0 ) {
                overlappedOldHint.sqlExecIds.addAll(this.sqlExecIds);
            }
        }

        return this.x;
    }

    @Override
    public String toString() {
        return "Hint{" +
               "x=" + x +
               ", y=" + y +
               ", widthPlusX=" + widthPlusX +
               ", heightPlusY=" + heightPlusY +
               ", sqlExecId=" + sqlExecIds +
               ", type=" + type +
               '}';
    }
}
